package com.smooth.site.sx.pc.jifen.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.dao.gen.BizGoodsMapper;
import com.smooth.dao.gen.BizOrderMapper;
import com.smooth.dao.gen.BizStudentMapper;
import com.smooth.dao.gen.bean.BizGoods;
import com.smooth.dao.gen.bean.BizOrder;
import com.smooth.dao.gen.bean.BizOrderExample;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.goods.service.BizGoodsService;
import com.smooth.site.sx.mag.order.service.BizOrderService;
import com.smooth.site.sx.pc.jifen.enums.GoodType;
import com.smooth.site.sx.pc.jifen.vo.SubOrderReq;
import com.smooth.sys.pc.vo.ListRsp;
import com.smooth.sys.site.biz.pay.enums.OrderStatus;

@Component
public class PcOrderService {
	@Autowired
	BizOrderService bizOrderService;
	@Autowired
	BizGoodsService bizGoodsService;
	@Autowired
	BizGoodsMapper bizGoodsMapper;
	@Autowired
	BizOrderMapper bizOrderMapper;
	@Autowired
	BizStudentMapper bizStudentMapper;
	@Autowired
	PcGoodsService pcGoodsService;
	@Transactional
	public BizOrder submitOrder(SubOrderReq subOrderReq,BizStudent bizStudent) {
		BizOrder bizOrder = new BizOrder();
		
		String goodNo = subOrderReq.getGoodNo();
		BizGoods bizGoods = bizGoodsMapper.selectByPrimaryKey(goodNo);
		
		Integer jifen = subOrderReq.getJifen();
		Integer goodNum = subOrderReq.getNum();
		if(!jifen.equals(goodNum*bizGoods.getPriceJifen())) {
			throw new SiteException(SiteCodeEnums.param_error,goodNo+"积分计算错误，请联系管理员");
		}
		
		GoodType goodType = GoodType.getStatus(bizGoods.getGoodType());
		if(GoodType.xuni.equals(goodType)) {
			throw new SiteException(SiteCodeEnums.param_error, "虚拟商品无须地址信息");
		}
		
		pcGoodsService.subGoodsStock(goodNo,goodNum);
		
		bizOrder.setAddressId(subOrderReq.getAddressId());
		bizOrder.setGoodId(goodNo);
		bizOrder.setPriceJifen(jifen);
		bizOrder.setGoodNum(goodNum);
		bizOrder.setGoodType(bizGoods.getGoodType());
		
		bizOrder.setOrderStatus(OrderStatus.PAYSUCCESS.value);
		bizOrder.setOrderStatusName(OrderStatus.PAYSUCCESS.name);
		
		bizOrder.setStuId(bizStudent.getId());
		bizOrder.setStuName(bizStudent.getStuName());
		bizOrder.setStuTelNo(bizStudent.getTelNo());
		BizOrder bo = bizOrderService.save(bizOrder);
		pcJifenService.subJifen(jifen,bizStudent,bo.getId(),bizGoods.getGoodTitle());
		return bo;
	}
	@Autowired
	PcJifenService pcJifenService;
	public BizOrder queryDetail(String orderNo) {
		return bizOrderService.get(orderNo);
	}
	public ListRsp<BizOrder> queryOrderList(String telNo,OrderStatus orderStatus) {
		ListRsp<BizOrder> lrsp = new ListRsp<BizOrder>();
		BizOrderExample bizOrderExample = new BizOrderExample();
		BizOrderExample.Criteria criteria = bizOrderExample.createCriteria();
		criteria.andStuTelNoEqualTo(telNo);
		if(orderStatus!=null) {
			criteria.andOrderStatusEqualTo(orderStatus.value);
		}
		List<BizOrder> olist = bizOrderMapper.selectByExample(bizOrderExample);
		lrsp.setDatalist(olist);
		lrsp.setTsize(bizOrderMapper.countByExample(bizOrderExample));
		return lrsp;
	}
	
	
}
